import http from '../common/request'
import {defineStore} from 'pinia';

export default defineStore('momentStore', {
    state: () => {
        return {
        moments: [],
        activeIndex: -1,
        currentCircle: {}
        }
    },
    actions: {

        setMoment(state, moments) {
            this.moments = moments;
        },

        activeMoment(state, index) {
            this.activeIndex = index;
        },

        addMoment(state, moments) {
            this.moments.unshift(moments);
        },

        removeMoment(state, momentId) {
            this.groups.forEach((g, index) => {
                if (g.id == momentId) {
                    state.moments.splice(index, 1);
                    if (state.activeIndex >= state.moments.length) {
                        state.activeIndex = state.moments.length - 1;
                    }
                }
            })
        },

        setCurrentCircle(state, circle) {
            this.currentCircle = circle;
        },

        clear(state) {
            this.moments = [];
            this.activemoment = -1;
        },
        loadMoment(context, circleId) {
            return new Promise((resolve, reject) => {
            http({
                    url: '/moment/list?circleId=' + circleId,
                    method: 'GET'
                }).then((moments) => {
                    context.commit("setMoment", moments);
                    resolve();
                }).catch((res) => {
                    reject(res);
            })
            });
        },
        getCurrentCircle(context, circleId) {
            return new Promise((resolve, reject) => {
                http({
                    url: '/circle/detail/' + circleId,
                    method: 'GET'
                }).then((circle) => {
                    context.commit("setCurrentCircle", circle);
                    resolve();
                }).catch((res) => {
                    reject(res);
                })
            });
        }
    },

})